<?php

/**
 * This is the model class for table "cat".
 *
 * The followings are the available columns in table 'cat':
 * @property string $id
 * @property string $name
 * @property string $name_en
 * @property string $alias
 * @property string $alias_en
 * @property string $post_count
 * @property string $post_count_en
 * @property integer $index
 *
 * The followings are the available model relations:
 * @property Post[] $posts
 */
class PostCat extends Model
{
    public $name_other = '';
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Cat the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'post_cat';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, name_en, alias, alias_en, index', 'required'),
			array('index', 'numerical', 'integerOnly'=>true),
			array('name, name_en, alias, alias_en', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, name_en, alias, alias_en, index', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'posts' => array(self::HAS_MANY, 'Post', 'cat_id'),
//            'new_4_posts' => array(self::HAS_MANY, 'Post', 'cat_id', 'order'=>'created DESC', 'limit'=> 4),
//			'rand_4_posts' => array(self::HAS_MANY, 'Post', 'cat_id', 'order'=>'RAND()', 'limit'=> 4),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'name_en' => 'Name En',
			'alias' => 'Alias',
			'alias_en' => 'Alias En',
			'index' => 'Index',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('name_en',$this->name_en,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('alias_en',$this->alias_en,true);
		$criteria->compare('index',$this->index);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    
    public function getData(){
        $cats = $this->findAll(new CDbCriteria(array('order' => '`index`')));
        return CHtml::listData($cats, 'id' , 'name');
    }
    
    public function getUrl(){
        return Yii::app()->createUrl('/web/post/index', array(
            'cat_alias'=> $this->lang == 'en' ? $this->alias_en : $this->alias,
            'lang'  => $this->lang,
        ));
    }
    
    public function getUrlOther(){
        return Yii::app()->createUrl('/web/post/index', array(
            'cat_alias'=> $this->lang == 'en' ? $this->alias : $this->alias_en,
            'lang'  => $this->lang == 'en' ? 'vi' : 'en',
        ));
    }
    
    /**
    * Detect news by lang
    * 
    * @param string $lang vi or en
    */
    public function detectByLang(){

        if($this->lang == 'en'){
            $this->name_other = $this->name;
            $this->name = $this->name_en;
        }else{
            $this->name_other = $this->name_en;
        }
    }
    
    public function updatePostCount(){
        $query = "UPDATE post_cat c SET 
        c.post_count = (SELECT COUNT(*) FROM post p WHERE p.cat_id = c.`id` AND p.status = 'ENABLE'),
        c.post_count_en = (SELECT COUNT(*) FROM post p WHERE p.cat_id = c.`id` AND p.status = 'ENABLE' AND has_en = 1);";
        Yii::app()->db->createCommand($query)->execute();  
    }
}